package cn.afactory.tygz.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.controller.CommunityController;
import cn.afactory.tygz.model.ScenicSpotsInfo;
import cn.afactory.tygz.model.ScenicSpotsInfoList;
import cn.afactory.tygz.ui.activities.ScenicSpotsDetailActivity;
import cn.afactory.tygz.ui.adapter.ScenicSpotsListAdapter;
import cn.afactory.tygz.ui.base.BaseFragment;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshBase;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshListView;
import cn.afactory.tygz.ui.base.utils.LocalUtils;
import cn.afactory.tygz.ui.base.utils.SPUtils;

public class ScenicSpotsListFragment extends BaseFragment {
    private static final String TAG = ScenicSpotsListFragment.class.getSimpleName();
    protected CommunityController mController;
    protected ScenicSpotsListAdapter mAdapter;
    private LayoutInflater minflater;
    protected int mPageIndex = 1;
    protected int mPageSize = 10;

    private List<ScenicSpotsInfo> mList = new ArrayList<>();
    public ScenicSpotsListFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mController = new CommunityController(mContext, mUIHandler, null);
    }

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container) {
        this.minflater = inflater;
        View mlayout = inflater.inflate(R.layout.fg_scenic_spots_list, null);
        initViews(mlayout);
        showTitleBar(false);
        return mlayout;
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case CommunityController.MSG_GET_SCENIC_SPOT_LIST_OK:
                if (msg.obj instanceof ScenicSpotsInfoList) {
                    hideErrorPage(-1);
                    ScenicSpotsInfoList t = (ScenicSpotsInfoList) msg.obj;
                    if (t.value != null && t.value.size() >= 0) {
                        if(mAdapter.getCount() == 0 && t.value.size() == 0){
                            showNullPage(true);
                            return ;
                        }
                        try {
                            mList.addAll(t.value);
                            mAdapter.setData(mList);
                            mPageIndex = (mList.size()/mPageSize) + 1;
                            setListListener();

                            if (plistview.isRefreshing()) {
                                plistview.onRefreshComplete();
                            }
                            if (t.value.size() < mPageSize) {
                                plistview.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                            } else {
                                plistview.setMode(PullToRefreshBase.Mode.BOTH);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        if (mAdapter.getCount() == 0) {
                            showNullPage(true);
                        }
                    }
                }
                break;
            case CommunityController.MSG_GET_SCENIC_SPOT_LIST_KO:
                if (mAdapter.getCount() == 0) {
                    hideErrorPage(-1);
                    showErrorPage(msg.arg1, new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            startLoadData(true);
                        }
                    });
                } else {
                    LocalUtils.showToast(mContext, (String) msg.obj);
                }
                break;
        }

        // 结束下拉刷新
        if (plistview.isRefreshing()) {
            plistview.onRefreshComplete();
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        startLoadData(true);
    }

    /**
     * 初始化组件 views
     *
     * @param view
     */
    private void initViews(View view) {
        plistview = (PullToRefreshListView) view.findViewById(R.id.plv_subject);
        initListView();
    }

    /**
     * 加载listview数据
     */
    protected void startLoadData(boolean cleanData) {
        if (cleanData) {
            mPageIndex = 1;
            mList.clear();
        }
        double lat = 0;
        double lng = 0;
        if(SPUtils.getExtraCurrentLat(getActivity()) != null){
            lat = Double.parseDouble(SPUtils.getExtraCurrentLat(getActivity()));
        }

        if(SPUtils.getExtraCurrentLon(getActivity()) != null){
            lng = Double.parseDouble(SPUtils.getExtraCurrentLon(getActivity()));
        }
        mController.loadScenicSpotsList(lat, lng, 0, mPageIndex, mPageSize);
    }

    /**
     * 初始化list view
     */
    private void initListView() {
        mAdapter = new ScenicSpotsListAdapter(mContext,null);
        plistview.setAdapter(mAdapter);
        mAdapter.setData(mList);
        plistview.setMode(PullToRefreshBase.Mode.BOTH);
        setListListener();
        plistview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                startLoadData(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                startLoadData(false);
            }
        });
    }

    protected void setListListener() {
        plistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListView lv = plistview.getRefreshableView();
                if (position >= lv.getHeaderViewsCount()) {
                    position -= lv.getHeaderViewsCount();
                }
                Intent intent = new Intent(mContext, ScenicSpotsDetailActivity.class);
                ScenicSpotsInfo scenicSpotsInfo = (ScenicSpotsInfo)mAdapter.getItem(position);
                intent.putExtra(ScenicSpotsDetailActivity.EXT_DATA_TAG, scenicSpotsInfo);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
